/*
 * @Author: your name
 * @Date: 2021-02-03 16:58:38
 * @LastEditTime: 2021-02-04 09:42:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wechat-node\model\folders.js
 */

// 文件夹模块
const sequelize = require('../config/operateDB');
const {Sequelize, DataTypes, Op} = require('sequelize');

const Folders = sequelize.define('folder', {
    id: {
        type: DataTypes.UUID,
        allowNull: false,
        primaryKey: true,
        defaultValue: Sequelize.UUIDV4,
    },
    name: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    companyId: {
        type: DataTypes.UUID,
        allowNull: false,
    },
    creatorId: {
        type: DataTypes.UUID,
        allowNull: false,
    },
}, {

});

async function initFolders() {
    await Folders.sync();
    const count = await Folders.count();
    if (0 === count) {
        await Folders.create({
            id: 'bf2d6778-6fc1-46ca-bc66-cc84a7ee4053',
            name: '管理员文件夹',
            companyId: 'a0a53019-820d-4253-8338-0e6fb09a5e7b',
            creatorId: '6e81b384-dce5-4d58-abf9-56bde96eda25',
            
        });

        console.log('初始化文件夹成功！');
    }
}
initFolders();

const foldersModel = {
    async getFolders(ctx) {
        const {companyId} = ctx.query;

        if (companyId) {
            const folders = await Folders.findAll(
                {
                    where: {
                        companyId: companyId
                    },
                    raw: true,
                }
            );
            ctx.pageRes(folders);
        } else {
            ctx.errorParams('又不传参数，小伙子！');
        }
        
    },

    async addFolders(ctx) {
        const params = ctx.request.body;
        const validFlag = this.validFolders(ctx, params);

        if (validFlag) {
            const poducts = await Folders.create({
                ...params,
                companyId: ctx.session.sessionData.company,
                creatorId: ctx.session.sessionData.id,
            });

            ctx.success('增加成功！', poducts);
        }
    },
    validFolders(ctx, params) {
        if (!params.name) {
            ctx.errorParams('文件夹名称不存在！');
            return false;
        } else {
            return true;
        }
    },
    async deleteFolders(ctx)  {
        const {User} = require('./users');
        if (!!ctx.request.body.id) {
            const deleteIds = (ctx.request.body.id || '').split(',');
            const users = await User.findAll({
                where: {
                    folderId: {
                        [Op.in]: deleteIds
                    }
                },
                raw: true,
            });
            if (0 !== users.length) {
                ctx.serviceError('当前文件夹下存在用户！', null);
                return ;
            }

            const delFolders = await Folders.destroy({
                where: {
                    id: {
                        [Op.in]: deleteIds,
                    },
                },
            });
            if (0 === delFolders) {
                ctx.errorParams('未找到指定id！');
            } else {
                ctx.success('删除成功！', {
                    id: deleteIds,
                });
            }

            
        } else {
            ctx.errorParams('id不存在！');
        }
    },
    async editFolders(ctx)  {
        const params = ctx.request.body;
        const editId = params.id;
        if (!editId) {
            ctx.errorParams('id不存在！');
        } else {
            // 
            const company = await Folders.findOne({
                where: {
                    id: editId
                },
                raw: true,
            });
            await Folders.update({
                ...company,
                ...params,
            }, {
                where: {
                    id: editId
                },
            });
            ctx.success('修改成功', {
                id: editId,
            });
        }
        
    }
    
};


module.exports = {
    foldersModel,
    Folders
};